package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.util.Result;
import com.woniuxy.core.util.StateNum;
import com.woniuxy.goods.dto.CatPictureListDto;
import com.woniuxy.goods.entity.GoodsCat;
import com.woniuxy.goods.entity.GoodsCatPicture;
import com.woniuxy.goods.form.AddCatPictureForm;
import com.woniuxy.goods.form.UpdateCatPictureByCatForm;
import com.woniuxy.goods.form.UpdateCatPictureForm;
import com.woniuxy.goods.param.AddCatPictureParam;
import com.woniuxy.goods.param.UpdateCatPictureByCatParam;
import com.woniuxy.goods.param.UpdateCatPictureParam;
import com.woniuxy.goods.service.GoodsCatPictureService;
import com.woniuxy.web.util.ConvertUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * 类别定义图控制层接口
 * @author ...
 * @since 2024年05月27日
 */
@RestController
@Api(tags = "类别定义图管理接口")
@RequestMapping("/goodsCatPicture")
public class GoodsCatPictureController {
    /**
     * 类别定义图服务层接口
     */
    @Resource
    private GoodsCatPictureService catPictureService;

    /**
     * 新增类别定义图
     * @param form 新增定义图对象
     * @return 定义图信息
     * @throws Exception
     */
    @ApiOperation("新增类别定义图")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatPicture.class)
    })
    @PostMapping("/addCatPicture")
    @Permission(roles = {"超级管理员","管理员"})
    public Result addCatPicture(@RequestBody AddCatPictureForm form)throws Exception{
        GoodsCatPicture catPicture = catPictureService.addCatPicture(BeanUtil.toBean(form, AddCatPictureParam.class));
        return Result.success("新增类别定义图成功",catPicture);
    }

    /**
     * 删除类别定义图
     * @param id 定义图id
     * @return
     * @throws Exception
     */
    @ApiOperation("删除类别定义图")
    @ApiImplicitParam("定义图id")
    @GetMapping("/delCatPicture")
    @Permission(roles = {"超级管理员","管理员"})
    public Result delCatPicture(Integer id)throws Exception{
        catPictureService.delCatPicture(id);
        return Result.success("删除类别定义图成功",null);
    }

    /**
     * 修改类别定义图
     * @param form 修改定义图对象
     * @return 定义图信息
     * @throws Exception
     */
    @ApiOperation("修改类别定义图")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatPicture.class)
    })
    @PostMapping("/updateCatPicture")
    @Permission(roles = {"超级管理员","管理员"})
    public Result updateCatPicture(@RequestBody UpdateCatPictureForm form)throws Exception{
        GoodsCatPicture catPicture = catPictureService.updateCatPicture(BeanUtil.toBean(form, UpdateCatPictureParam.class));
        return Result.success("修改类别定义图成功",catPicture);
    }

    /**
     * 根据类别id修改类别定义图
     * @param form 修改定义图对象
     * @return 定义图信息
     * @throws Exception
     */
    @ApiOperation("根据类别id修改类别定义图")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatPicture.class)
    })
    @PostMapping("/updateCatPictureByCat")
    @Permission(roles = {"超级管理员","管理员"})
    public Result updateCatPicture(@RequestBody UpdateCatPictureByCatForm form)throws Exception{
        UpdateCatPictureByCatParam param = BeanUtil.toBean(form, UpdateCatPictureByCatParam.class);
        List<AddCatPictureParam> params = ConvertUtil.convertListTo(form.getForms(), AddCatPictureParam::new);
        param.setParams(params);
        List<GoodsCatPicture> catPicture = catPictureService.updateCatPictureByCat(param);
        return Result.success("根据类别id修改类别定义图成功",catPicture);
    }

    /**
     * 根据id查询类别定义图
     * @param id 定义图id
     * @return 定义图信息
     * @throws Exception
     */
    @ApiOperation("根据id查询类别定义图")
    @ApiImplicitParam("定义图id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatPicture.class)
    })
    @GetMapping("/findCatPictureById")
    public Result findCatPictureById(Integer id)throws Exception{
        GoodsCatPicture catPicture = catPictureService.findCatPictureById(id);
        return Result.success("根据id查询类别定义图成功",catPicture);
    }

    /**
     * 根据类别id查询类别定义图
     * @param catId 类别id
     * @return 定义图信息集合
     * @throws Exception
     */
    @ApiOperation("根据类别id查询类别定义图")
    @ApiImplicitParam("类别id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatPicture.class)
    })
    @GetMapping("/findCatPictureByCatId")
    public Result findCatPictureByCatId(Integer catId)throws Exception{
        CatPictureListDto catPictures = catPictureService.findCatPictureByCatId(catId);
        return Result.success("根据类别id查询类别定义图成功",catPictures);
    }

    /**
     * 根据类别属性组id查询类别定义图
     * @param id 类别属性组id
     * @return 定义图信息集合
     * @throws Exception
     */
    @ApiOperation("根据类别属性组id查询类别定义图")
    @ApiImplicitParam("类别属性组id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatPicture.class)
    })
    @GetMapping("/findPictureById")
    public Result findPictureById(Integer id)throws Exception{
        CatPictureListDto catPictures = catPictureService.findPictureById(id);
        return Result.success("根据类别属性组id查询类别定义图成功",catPictures);
    }

    /**
     * 根查询类别自己的定义图
     * @param catId 类别id
     * @return 定义图信息集合
     * @throws Exception
     */
    @ApiOperation("根查询类别自己的定义图")
    @ApiImplicitParam("类别id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsCatPicture.class)
    })
    @GetMapping("/findPictureByCat")
    public Result findPictureByCat(Integer catId)throws Exception{
        List<GoodsCatPicture> catPictures = catPictureService.findPictureByCat(catId);
        return Result.success("根查询类别自己的定义图成功",catPictures);
    }

}

